package com.lbb.simple.demo.http

import android.util.Log
import com.lbb.http.GsonUtil

import com.lbb.http.bean.ResultBean
import com.lbb.http.bean.ResultException
import com.lbb.http.interceptor.ResultInterceptor

class GsonResultInterceptor : ResultInterceptor {

    override fun onResponse(string: String): String {
        Log.i("GsonInterceptor", "onResponse--->$string")
        val bean = GsonUtil.toBean(string, ResultBean::class.java)
        bean?.let {
            if (bean.code == "0000") {
                return GsonUtil.toJson(bean.result).toString()
            }
            throw  ResultException(bean.message)
        }
        throw  ResultException("未知错误")
    }

    override fun onFailure(string: String): String {
        Log.i("GsonInterceptor", "onFailure--->$string")
        return string
    }
}